package com.cloudkinto.controller.sc;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.vo.sc.ScFarawayInfoPageVo;
import com.cloudkinto.vo.sc.ScFarawayPageVo;
import com.cloudkinto.service.sc.ScFarawayInfoRuleService;
import com.cloudkinto.vo.sc.faraway.*;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.sc.ScFarawayRuleService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.cloudkinto.base.BaseController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 偏远地区规则 前端控制器
 * </p>
 * @author hua
 * @since 2024-11-05
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "偏远地区规则omp模块")
@RequestMapping("/api/admin/omp/ScFarawayRule")
public class ScFarawayRuleController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ScFarawayRuleController.class);

    private final ScFarawayRuleService service;
    private final ScFarawayInfoRuleService farawayInfoRuleService;


    @GetMapping("/addInit")
    @ApiOperation(value = "偏远地区规则新增初始化", notes = "偏远地区规则新增初始化")
    public SingleResult addInit() {
        return service.addInit();
    }

    @UserSessionRequired
    @PostMapping
    @ApiOperation(value = "偏远地区规则新增", notes = "偏远地区规则新增")
    public SingleResult add(@RequestBody @Validated FarawayAddReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("新增成功",service.add(req,userId, getTenantId()));
    }


    @UserSessionRequired
    @PostMapping("/addDetail")
    @ApiOperation(value = "新增偏远明细", notes = "新增")
    public SingleResult addDetail(@RequestBody @Validated FarawayInfoRuleAddReq req) {
        return SingleResult.success("新增成功", service.addInfoRule(req, getCurrentUserId(), true));
    }



    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "偏远地区规则编辑初始化", notes = "偏远地区规则编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id);
    }

    @UserSessionRequired
    @PutMapping
    @ApiOperation(value = "偏远地区规则修改", notes = "偏远地区规则修改")
    public SingleResult update(@RequestBody @Validated FarawayUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功",service.update(req,userId, getTenantId()));
    }


    @UserSessionRequired
    @PutMapping("/updateDetail")
    @ApiOperation(value = "修改偏远明细", notes = "修改偏远明细")
    public SingleResult updateDetail(@RequestBody @Validated FarawayInfoRuleUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.updateInfoRule(req, userId, true));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "偏远地区规则根据id删除", notes = "偏远地区规则根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功",service.delete(id, userId, getTenantId()));
    }


    @UserSessionRequired
    @DeleteMapping("/deleteDetail/{id}")
    @ApiOperation(value = "删除偏远明细", notes = "删除偏远明细")
    public SingleResult deleteDetail(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", farawayInfoRuleService.delete(id, userId));
    }


    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "偏远地区规则根据id查看详情", notes = "偏远地区规则根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功",service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "偏远地区规则查询初始化", notes = "偏远地区规则查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @UserSessionRequired
    @GetMapping("/list")
    @ApiOperation(value = "偏远地区规则查询", notes = "偏远地区规则查询")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = ScFarawayPageVo.class))
    public SingleResult list(HttpServletRequest request){
        Map<String, Object> map = this.getFarawayMap(request);
        return SingleResult.success("查询成功", service.getList(map));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "偏远地区规则查询（分页）", notes = "偏远地区规则查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "farawayRuleType", value = "类型"),
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "farawayRuleName", value = "farawayRuleName"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = ScFarawayPageVo.class))
    public PageResult listPage(HttpServletRequest request){
        Map<String, Object> map = this.getFarawayMap(request);
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }

    public Map<String, Object> getFarawayMap(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String farawayRuleType = request.getParameter("farawayRuleType");
        String status = request.getParameter("status");
        String farawayRuleName = request.getParameter("farawayRuleName");
        map.put("farawayRuleType", farawayRuleType);
        map.put("status", status);
        map.put("farawayRuleName", farawayRuleName);
        map.put("tenantId", getTenantId());
        return map;
    }


    @UserSessionRequired
    @GetMapping("/detailPageList")
    @ApiOperation(value = "偏远地区规则详情查询（分页）", notes = "偏远地区规则详情查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "farawayRuleId", value = "偏远明细主表id"),
            @ApiImplicitParam(name = "countryCode", value = "国家地区", type = "arr"),
            @ApiImplicitParam(name = "state", value = "省"),
            @ApiImplicitParam(name = "city", value = "城市"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = ScFarawayInfoPageVo.class))
    public PageResult detailPageList(HttpServletRequest request){

        Map<String, Object> map = new HashMap<>();
        String farawayRuleId = request.getParameter("farawayRuleId");
        String[] countryCode = request.getParameterValues("countryCode");
        String state = request.getParameter("state");
        String city = request.getParameter("city");
        if (countryCode != null) {
            map.put("countryCode", Arrays.asList(countryCode));
        }
        map.put("state", state);
        map.put("city", city);

        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("farawayRuleId", farawayRuleId);
        map.put("current", current);
        map.put("pageSize", size);
        return farawayInfoRuleService.getListPage(map);
    }



    @UserSessionRequired
    @PostMapping("/importDetailRule")
    @ApiOperation(value = "导入偏远明细")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = FarawayRuleImportRes.class))
    public SingleResult importDetailRule(MultipartFile file, HttpServletRequest request) {
        String farawayRuleId = request.getParameter("farawayRuleId");
        FarawayRuleImportRes zoneTempImportRes = service.importRuleDetail(file, Long.parseLong(farawayRuleId), getCurrentUserId(), getTenantId());
        return SingleResult.success(zoneTempImportRes);
    }

    @UserSessionRequired
    @GetMapping("/exportDetailRule")
    @ApiOperation(value = "导出偏远明细", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "farawayRuleId", value = "偏远明细id"),
    })
    public void exportDetailRule(HttpServletRequest request, HttpServletResponse response) {
        String farawayRuleId = request.getParameter("farawayRuleId");
        service.exportDetailRule(farawayRuleId, response);
    }

}

